<?php

//	20090211
// 	TODO: implement statuses


function GetLangString($lang,$type){
	if ($type=="") $type = "txt_na";
	//global $SITE_MIDDLE	;
	$query        = "SELECT `text` from `text` where `lang`='$lang' and `type`='$type'";
	$result = mysql_query($query);
  $num = mysql_num_rows($result);
	if ($num){ 
    if ($num > 1) $SITE_MIDDLE .= "Text Double Translated!!  Language: $lang  Text:$type<br>";
		return mysql_result($result,0);
	}	
	else //$SITE_MIDDLE .= "Text Not Translated!!  Language: $lang  Text:$type<br>";
	return("($lang/$type)");
}


function SecurityCheck($string) {
	if (stristr($string,"<?")) return true;				// allow no php injection
	if (stristr($string,"<script")) return true;	// allow no javascript
	//if (stristr($string,"<style")) return true; 	// allow no style overrides
	return false;
}

function ExtraSecurityCheck($string) {
	if (stristr($string,"<")) return true;					// allow no html
	if (stristr($string,"[")) return true;					// allow no bb
	if (stristr($string,"http")) return true;				// allow no http
	if (stristr($string,"www")) return true;				// allow no www
	return false;
}

function FormatElement($title,$text){
	global $BBHandler;
	return  "<div class=\"element\"><div class=\"label\">$title</div>$text</div>";
}

function FormatSmallElement($title,$text){
	global $BBHandler;
	return  "<div class=\"element\"><div class=\"smalllabel\">$title</div>$text</div>";
}

function FormatElementTime($title,$time,$text){
	global $BBHandler;
	return  "<div class=\"element\"><div class=\"label\">$title</div><div class=\"smalllabel\">$time</div>$text</div>";
}

function FormatLink($title,$subtitle,$url){
	return 
"<div class=\"forumelement\">
	<div class=\"forumtitle\">
		<a href=\"$url\">$title
	</div>
		<div class=\"forumsubtitle\">
		$subtitle
		</a>
	</div>
</div>";
}

function SendMail($to_name, $to_email, $subject, $message) {

	global $SITE_NAME,$SITE_EMAIL;
	$headers  = "From: $SITE_NAME <$SITE_EMAIL>\r\n";
	$headers .= "Date: " . date("r") . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=UTF-8\r\n";
	$to = "$to_name <$to_email>";
	if(!mail($to, $subject, $message, $headers)) {
		FormatMessage(GetLangString($lang,$txt_err),GetLangString($lang,$msg_mailerr));
	}
} 

function SendVerification($name,$signup,$email){
	global $SITE_URL,$SITE_NAME,$lang;
	$code = md5($signup.$name);
	$url	= "<a href=\"$SITE_URL/index.php?do=verify&name=$name&code=$code\">
										$SITE_URL/index.php?do=verify&name=$name&code=$code</a>";

	$message= str_ireplace("%site_name%",$SITE_NAME,
						str_ireplace("%url%",      $url,
						str_ireplace("%name%",     $name,
						GetLangString($lang,"mail_activation"))));
/*
	$message = "Welcome to BlaatSchaap $name<br><br>\r\n
							To verify your account, please visit <a href=\"$SITE_URL/index.php?do=verify&name=$name&code=$code\">\r\n 
							$SITE_URL/index.php?do=verify&name=$name&code=$code</a>	";
*/	

	SendMail($name,$email,"Email verification",$message);  
}

function GetStatusText ($status) {
	include ("status.php");	
return ("Implement me!!!");
}

function GetStatusImg ($status)  {
	include ("status.php");
	$icons = array(); 
	/*
	$STA_MEMBER			=		0x00000001;
	$STA_BANNED			=		0x00000002;

	$STA_ADMIN			=		0x00000010;
	$STA_MOD				=		0x00000020;

	$STA_DJ					=		0x00000100;
	$STA_CODER			=		0x00000200;
	$STA_TRANS			=		0x00000400;
	$STA_BAND				=		0x00000800;


	$STA_1ST				=		0x00001000;
	$STA_PAY				=		0x00002000;
	$STA_ERE				=		0x00004000;

*/
	global $lang;

	if ($status & $STA_ADMIN) 		array_push($icons,array(GetLangString($lang,"txt_admin")		,"admin.png"));
	if ($status & $STA_MOD) 			array_push($icons,array(GetLangString($lang,"txt_mod"),"mod.png"));


	if ($status & $STA_DJ) 		array_push($icons,array(GetLangString($lang,"txt_dj")		,"dj.png"));
	if ($status & $STA_CODER) 		array_push($icons,array(GetLangString($lang,"txt_coder")		,"coder.png"));
	if ($status & $STA_TRANS) array_push($icons,array(GetLangString($lang,"txt_translater"),"trans.png"));
	if ($status & $STA_BAND) array_push($icons,array(GetLangString($lang,"txt_band"),"band.png"));

	if ($status & $STA_OLD) array_push($icons,array(GetLangString($lang,"txt_old"),"oldmember.png"));
	if ($status & $STA_PAY) array_push($icons,array(GetLangString($lang,"txt_pay"),"paymember.png"));
	if ($status & $STA_ERE) array_push($icons,array(GetLangString($lang,"txt_ere"),"eremember.png"));


	foreach ($icons as $icon) {
		$statusimg .= "<img border=\"0\" title=\"".$icon[0]."\"src=\"/images/".$icon[1]."\">"; 
	}


// 
	return $statusimg;
}

function text2html($text){
	return str_ireplace("\n","<br>",$text);
}

function html2text($text){
	return str_ireplace("<br>","\n",$text);
}

function bb2html($text){
	//global $BBHandler;
	// some weird bug in the bb_parse code preventing me from using tag i but the same code works with a different letter!!!! weirdness!!!
	//return text2html(bbcode_parse($BBHandler,str_ireplace("[/i]","[/j]",str_ireplace("[i]","[j]",$text))));

	// eeps it's a class!!! 
	$text = text2html(stripslashes($text)) ;
	$bbcode = new bbcode();
	$text = $bbcode->parse($text." "); //it seems to truncate the message by one bye, adding a space
	return $text;
}


function CheckEmail($email) {
	// eeps it's a class!!! 
	$validator = new EmailAddressValidator;
  return $validator->check_email_address($email);
}

function CountFriendRequests() {
	global $id;
	$wanabe=0;
	$query1        = "SELECT user1 from friends where user2='$id'";
	$result1 = mysql_query($query1);
	if ($res = mysql_num_rows($result1)){ 
		$i=0;
		while ($res > $i){
			$friendid = mysql_result($result1,$i++);
			$query2       = "SELECT * from friends where user1='$id' and user2='$friendid'";
			$isfriend 	= mysql_query($query2);
			if (!mysql_num_rows($isfriend)) {
				$wanabe++;
			}
		}
	}
	return $wanabe;
}

function GetEnum ( $table , $field){
	$query = "SHOW COLUMNS FROM `$table` LIKE '$field'";	
	$result = mysql_query($query);
	$return = mysql_fetch_array($result);
	$return = $return['Type'];
	$begin = stripos ($return, "(");
 	$end   = stripos ($return, ")");
	$return  = substr  ($return, $begin+1 , ($end-$begin)-1);
	$return  = str_replace ("'","",$return);		
	return 		explode (",",$return);		
}

function escapestr($str){
	if (!(get_magic_quotes_gpc())) return addslashes($str); return $str;
}

function GetDataFromURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);    
        return $output;
}
?>
